package com.itinsys.enma.web;

import java.sql.Blob;
import java.sql.SQLException;

import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.lob.SerializableBlob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itinsys.enma.domain.Environment;
import com.itinsys.enma.domain.dto.EnvironmentDTO;
import com.itinsys.enma.domain.response.EnvironmentResponse;
import com.itinsys.enma.domain.response.ResourceResponse;
import com.itinsys.enma.manager.EnvironmentManager;
import com.itinsys.enma.manager.PriorityEnvironmentManager;
import com.thoughtworks.xstream.core.util.Base64Encoder;



@Controller
@RequestMapping({ "/environment" })
public class EnvironmentFormController extends FormController {
	protected final Log logger = LogFactory.getLog("controller");

	@Autowired
	private EnvironmentManager environmentManager;
	
	@Autowired
	private PriorityEnvironmentManager priorityEnvironmentManager;

	
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	@ResponseBody
	public EnvironmentResponse getAllEnvironment() {
		return environmentManager.getAllEnvironment();
		
	}

	@RequestMapping(value = { "/add" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public EnvironmentResponse addEnvironment(@RequestBody EnvironmentDTO environmentDTO,HttpServletResponse response) throws SerialException, SQLException {
		Environment environment = environmentDTOToEnvironment(environmentDTO);

		EnvironmentResponse resp = null;
		resp = this.environmentManager.addEnvironment(environment);
		return resp;
	}

	private Environment environmentDTOToEnvironment(EnvironmentDTO environmentDTO) throws SerialException, SQLException {
		Environment environment = new Environment();
		environment.setBuilding(environmentDTO.getBuilding());
		environment.setCapacity(environmentDTO.getCapacity());
		environment.setDescription(environmentDTO.getDescription());
		environment.setActive(environmentDTO.isActive());
		environment.setResources(environmentDTO.getResources());
		if(environmentDTO.getLocation()!=null&&environmentDTO.getLocation().length()>0){
			Base64Encoder decoder = new Base64Encoder();
	        byte[] imageByte = decoder.decode(environmentDTO.getLocation());
			Blob blob = new SerialBlob(imageByte);
			environment.setLocation(new SerializableBlob(blob));
		}
		return environment;
	}

	@RequestMapping(value = { "/edit" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public EnvironmentResponse editEnvironment(	@RequestBody EnvironmentDTO environmentDTO,	HttpServletResponse response) throws SerialException, SQLException {
		Environment env=environmentDTOToEnvironment(environmentDTO);
		env.setId(environmentDTO.getId());
		return environmentManager.editEnvironment(env);
	}
	
	@RequestMapping(value = { "/delete" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public EnvironmentResponse deleteEnvironment(@RequestBody EnvironmentDTO environmentDTO,	HttpServletResponse response) throws SerialException, SQLException {
		Environment env=environmentDTOToEnvironment(environmentDTO);
		env.setId(environmentDTO.getId());
		return environmentManager.deleteEnvironment(env);
	}
	
	@RequestMapping(value = { "/getResourcesByEnvironment" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public ResourceResponse getResourcesByEnvironment(@RequestBody int environmentId,	HttpServletResponse response) {
		return environmentManager.getResourcesByEnvironment(environmentId);
	}
}
